<?php

namespace App\Service\delivery;
use _PHPStan_adbc35a1c\Nette\Neon\Exception;
use App\Model\Applet;
use App\Model\AppletCompany;
use App\Model\AppletPlayletRel;
use App\Model\Company;
use App\Model\PlayletEpisodes;
use App\Model\RechargeGoods;
use App\Model\RechargeTemplate;
use App\Model\RechargeTemplateGoodsRel;
use App\Service\CommicService;
use App\Model\Playlet;
use Hyperf\DbConnection\Db;
use Hyperf\Utils\ApplicationContext;
use App\Common\Response\SystemCode;
class RechargeTemplateService extends CommicService
{
    const cacheDuration = 60 * 15;
    public function __construct()
    {
        $container = ApplicationContext::getContainer();
        $this->redisService = $container->get(\Hyperf\Redis\Redis::class);

        $this->userAppletIds = $this->getUserAppletInfo();
    }
    const STATUS_ENABLE  = 1;
    public function Search(Array $params){

        $keyword     = $params['keyword'] ?? '';
        $company_id  = $params['company_id'] ?? 0;
        //$applet_id   = $params['applet_id'] ?? 0;
        $pageSize    = $params['pageSize'] ?? 10;
        $page        = $params['page'] ?? 1;
        $applet_ids     = $params['applet_ids'] ?? '';
        $delivery_user_id  = $params['delivery_user_id'] ?? '';
        if(empty($applet_ids)){
            return [];
        }
//        var_dump(123);
        //获取所拥有的
        if($applet_ids == '1'){
            //获取所有的小程序所绑定的短剧
            $applet_id =  $this->userAppletIds;
        }else{
            $applet_id =  [$applet_ids];
        }
        $query = RechargeTemplate::query()
            ->select(['recharge_template.id','recharge_template.name','recharge_template.status','recharge_template.type',
                'recharge_template.applet_id','recharge_template.company_id','recharge_template.created_at','recharge_template.updated_at'
                ,'applet.name as applet_name','system_company.name as company_name'
            ]);

        if ($keyword) {
            $query->orWhere([['recharge_template.name', 'like', '%'.$keyword.'%']]);
        }

        if(!empty($applet_id)){
            $query->whereIn('recharge_template.applet_id',$applet_id);
        }
        //根据用户获取ID
        $userInfo = $this->getUserAppletInfo(2);
        if($userInfo['role']['is_admin'] != 1){
            $query->where('recharge_template.user_id',$userInfo['id']);
        }
        $query->orwhere('recharge_template.id','1');
        $models = $query->orderBy('recharge_template.id','desc')
            ->leftJoin('applet','recharge_template.applet_id','=','applet.id')
            ->leftJoin('system_company','recharge_template.company_id','=','system_company.id')
            ->orderBy('recharge_template.id','desc')->paginate($pageSize,['*'],'page',$page);
        $items = $models->items();
        $selectData = [];
        foreach ($items as $item){
            $data = $item->toArray();
            $data['status_name']    =   self::$commonStatus[$item->status];
            $templateGoods      = $item->template;
            $member = array();
            $convention = array();
            $promotion = array();
            if(!empty($templateGoods)){
                foreach ($templateGoods as $goods){
                    if($goods['type'] == 1){
                        $convention[] = '充 '.(sprintf("%.2f",$goods['amount']/100)).' 元 '.'获得'.$goods['ml_coin'].' + '.$goods['give_coin'].' 币';
                    }
                    if($goods['type'] == 3 && $goods['status'] == 1){
                        $member[] = '充 '.(sprintf("%.2f",$goods['amount']/100)).' 元 '.$goods['name'];
                    }
                    if($goods['type'] == 2 && $goods['status'] == 1){

                        $promotion[] = '充 '.(sprintf("%.2f",$goods['amount']/100)).' 元 '.$goods['name'];

                    }
                }
            }
//            if(!empty($item->applet)){
//                $data['applet_name']  = $item->applet->name;
//            }else{
//                $data['applet_name']  = '';
//            }
//            if(!empty($item->company)){
//                $data['company_name']  = $item->company->name;
//            }else{
//                $data['company_name']  = '';
//            }
            $data['convention'] = $convention;
            $data['member'] = $member;
            $data['promotion'] = $promotion;
            $data['template_type_name'] = self::$templateType[$item->type];


            $selectData[] =$data;
        }
        $data = $models->toArray();
        $data['data'] = $selectData;

        return $data;
    }


    public function createOrUpdate(Array $postData, bool $is_update = false, RechargeTemplate $rechargeTemplateInfo = null){

        $name = $postData['name'];
        $status = $postData['status'];
        $applet_id = $postData['applet_id'];
        $temp_type = $postData['type'];
        //根据用户获取ID
        $userInfo = $this->getUserAppletInfo(2);
        $company_id = $userInfo['company_id'];

        $templateStatus = [true => 1,false => 0];

        $model = !empty($rechargeTemplateInfo)? $rechargeTemplateInfo : new RechargeTemplate();
        $model->name = $name;
        $model->user_id = $userInfo['id'];
        $model->status = $status;
        Db::beginTransaction();

        try {

            //查看公司是否存在
            $companyData = Company::query()->where(['id' => $company_id])->exists();
            if(empty($companyData)){
                throw new \Exception('公司不存在');
            }
            //查找小程序是否存在
            $appletData = Applet::query()->where(['id' => $applet_id])->exists();
            if(empty($appletData)){
                throw new \Exception('请选择小程序');
            }
            //查看小程序和公司是否存在绑定关系
            $appletCompanyData = AppletCompany::query()->where(['company_id' => $company_id,'applet_id' => $applet_id])->exists();
            if(empty($appletCompanyData)){
                throw new \Exception('公司和小程序不存在绑定关系小程序');
            }
            $model->applet_id = $applet_id;
            $model->company_id = $company_id;
            $model->type = $temp_type;
            if($model->save()){
                $template_id = $model->id;
                if($temp_type == 1) {

                    $conventions = $postData['convention'];
                    if (empty($conventions)) {
                        throw new \Exception('常规档位数据为空');
                    }
                    $members = $postData['member'];
                    if (empty($members)) {
                        throw new \Exception('会员档位数据为空');
                    }
                    $promotions = $postData['promotion'];
                    if (empty($promotions)) {
                        throw new \Exception('促销档位数据为空');
                    }
                    $sortInfo = $postData['sortInfo'];
                    if (empty($sortInfo)) {
                        throw new \Exception('播放页充值页面设置不能为空');
                    }
                    //保存模板到rechargeGoods 中
                    if ($is_update) {
                        //获取旧的ID
                        $rechargeGoodsIds = RechargeGoods::query()->where(['recharge_template_id' => $model->id, 'type' => 1])->pluck('id')->toArray();
                        $conventionIds = [];
                        foreach ($conventions as $convention) {
                            if (empty($convention['amount'])) {
                                throw new \Exception($convention['name'] . ':充值价格不能为空');
                            }
                            $amount = bcmul($convention['amount'], 100);
                            $amount = sprintf("%.2f", $amount);


                            $name = $convention['name'] ?? '';
                            $give_coin = $convention['give_coin'] ?? '';
                            $status = $templateStatus[$convention['status']] ?? 0;
                            $day = $convention['day'] ?? 0;
                            $value = $convention['value'] ?? '';
                            $bgtype = $convention['bgtype'] ?? '';
                            $classtext = $convention['classtext'] ?? '';
                            $classcolor = $convention['classcolor'] ?? '';
                            $foottext = $convention['foottext'] ?? '';
                            $footcolor = $convention['footcolor'] ?? '';
                            $footicon = $convention['footicon'] ?? '';
                            $giftbgc = $convention['giftbgc'] ?? '';
                            $giftcolor = $convention['giftcolor'] ?? '';
                            $classbgc = $convention['classbgc'] ?? '';
                            $amountcolor = $convention['amountcolor'] ?? '';
                            $type = 1;


                            if (empty($convention['id'])) {
                                RechargeGoods::create(
                                    [
                                        'name' => $name, 'amount' => $amount, 'ml_coin' => $amount,
                                        'give_coin' => $give_coin, 'type' => $type, 'status' => $status,
                                        'day' => $day, 'recharge_template_id' => $template_id, 'value' => $value,
                                        'bgtype' => $bgtype, 'classtext' => $classtext, 'classcolor' => $classcolor, 'foottext' => $foottext,
                                        'footcolor' => $footcolor, 'footicon' => $footicon, 'giftbgc' => $giftbgc, 'giftcolor' => $giftcolor,
                                        'classbgc' => $classbgc, 'amountcolor' => $amountcolor
                                    ]
                                );
                            } else {
                                $conventionIds[] = $convention['id'];
                                $updateData = [
                                    'amount' => $amount,
                                    'ml_coin' => $amount,
                                    'give_coin' => $give_coin,
                                    'status' => $status,
                                    'bgtype' => $bgtype, 'classtext' => $classtext, 'classcolor' => $classcolor, 'foottext' => $foottext,
                                    'footcolor' => $footcolor, 'footicon' => $footicon, 'giftbgc' => $giftbgc, 'giftcolor' => $giftcolor,
                                    'classbgc' => $classbgc, 'amountcolor' => $amountcolor
                                ];
                                RechargeGoods::query()->where(['id' => $convention['id']])->update($updateData);
                            }
                        }
                        if (!empty($rechargeGoodsIds)) {
                            $deleteGoodsId = array_diff($rechargeGoodsIds, $conventionIds);
                            if (!empty($deleteGoodsId)) {
                                RechargeGoods::query()->whereIn('id', $deleteGoodsId)->delete();
                            }
                        }


                        foreach ($members as $member) {
                            if (empty($member['amount']) && $member['status'] == 1) {
                                throw new \Exception($member['name'] . ':充值价格不能为空');
                            }
                            $amount = bcmul($member['amount'], 100);
                            $bgtype = $member['bgtype'] ?? '';
                            $classtext = $member['classtext'] ?? '';
                            $classcolor = $member['classcolor'] ?? '';
                            $foottext = $member['foottext'] ?? '';
                            $footcolor = $member['footcolor'] ?? '';
                            $footicon = $member['footicon'] ?? '';
                            $giftbgc = $member['giftbgc'] ?? '';
                            $giftcolor = $member['giftcolor'] ?? '';
                            $classbgc = $member['classbgc'] ?? '';
                            $amountcolor = $member['amountcolor'] ?? '';
                            if (empty($member['id'])) {
                                throw new \Exception($member['name'] . ':ID不存在');
                            }
                            $updateData = ['amount' => $amount,
                                'status' => $templateStatus[$member['status']],
                                'bgtype' => $bgtype, 'classtext' => $classtext, 'classcolor' => $classcolor, 'foottext' => $foottext,
                                'footcolor' => $footcolor, 'footicon' => $footicon, 'giftbgc' => $giftbgc, 'giftcolor' => $giftcolor,
                                'classbgc' => $classbgc, 'amountcolor' => $amountcolor
                            ];
                            //                        var_dump($member['id']);
                            //                        var_dump($amount);
                            RechargeGoods::query()->where(['id' => $member['id']])->update($updateData);

                        }
                        //                    exit();
                        foreach ($promotions as $promotion) {
                            if (empty($promotion['amount']) && $promotion['status'] == 1) {
                                throw new \Exception($promotion['name'] . ':充值价格不能为空');
                            }
                            $amount = bcmul($promotion['amount'], 100);
                            $bgtype = $promotion['bgtype'] ?? '';
                            $classtext = $promotion['classtext'] ?? '';
                            $classcolor = $promotion['classcolor'] ?? '';
                            $foottext = $promotion['foottext'] ?? '';
                            $footcolor = $promotion['footcolor'] ?? '';
                            $footicon = $promotion['footicon'] ?? '';
                            $giftbgc = $promotion['giftbgc'] ?? '';
                            $giftcolor = $promotion['giftcolor'] ?? '';
                            $classbgc = $promotion['classbgc'] ?? '';
                            $amountcolor = $promotion['amountcolor'] ?? '';
                            $updateData = ['amount' => $amount,
                                'status' => $templateStatus[$promotion['status']],
                                'bgtype' => $bgtype, 'classtext' => $classtext, 'classcolor' => $classcolor, 'foottext' => $foottext,
                                'footcolor' => $footcolor, 'footicon' => $footicon, 'giftbgc' => $giftbgc, 'giftcolor' => $giftcolor,
                                'classbgc' => $classbgc, 'amountcolor' => $amountcolor

                            ];
                            RechargeGoods::query()->where(['id' => $promotion['id']])->update($updateData);
                        }
                    } else {

                        foreach ($conventions as $convention) {
                            if (empty($convention['amount'])) {
                                throw new \Exception($convention['name'] . ':充值价格不能为空');
                            }
                            $amount = bcmul($convention['amount'], 100);
                            $name = $convention['name'] ?? '';
                            $give_coin = $convention['give_coin'] ?? '';
                            $status = $templateStatus[$convention['status']] ?? 0;
                            $type = 1;
                            $day = $convention['day'] ?? 0;
                            $bgtype = $convention['bgtype'] ?? '';
                            $classtext = $convention['classtext'] ?? '';
                            $classcolor = $convention['classcolor'] ?? '';
                            $foottext = $convention['foottext'] ?? '';
                            $footcolor = $convention['footcolor'] ?? '';
                            $footicon = $convention['footicon'] ?? '';
                            $giftbgc = $convention['giftbgc'] ?? '';
                            $giftcolor = $convention['giftcolor'] ?? '';
                            $classbgc = $convention['classbgc'] ?? '';
                            $amountcolor = $convention['amountcolor'] ?? '';
                            RechargeGoods::create(
                                [
                                    'name' => $name, 'amount' => $amount, 'ml_coin' => $amount,
                                    'give_coin' => $give_coin, 'type' => $type, 'status' => $status,
                                    'day' => $day, 'recharge_template_id' => $template_id, 'value' => $convention['value'],
                                    'bgtype' => $bgtype, 'classtext' => $classtext, 'classcolor' => $classcolor, 'foottext' => $foottext,
                                    'footcolor' => $footcolor, 'footicon' => $footicon, 'giftbgc' => $giftbgc, 'giftcolor' => $giftcolor,
                                    'classbgc' => $classbgc, 'amountcolor' => $amountcolor

                                ]
                            );
                        }

                        foreach ($members as $member) {
                            if (empty($member['amount']) && $member['status'] == 1) {
                                throw new \Exception($member['name'] . ':充值价格不能为空');
                            }
                            $amount = bcmul($member['amount'], 100);
                            $bgtype = $member['bgtype'] ?? '';
                            $classtext = $member['classtext'] ?? '';
                            $classcolor = $member['classcolor'] ?? '';
                            $foottext = $member['foottext'] ?? '';
                            $footcolor = $member['footcolor'] ?? '';
                            $footicon = $member['footicon'] ?? '';
                            $giftbgc = $member['giftbgc'] ?? '';
                            $giftcolor = $member['giftcolor'] ?? '';
                            $classbgc = $member['classbgc'] ?? '';
                            $amountcolor = $member['amountcolor'] ?? '';
                            RechargeGoods::create(
                                [
                                    'name' => $member['name'], 'amount' => $amount, 'type' => 3, 'status' => $templateStatus[$member['status']],
                                    'day' => $member['day'], 'recharge_template_id' => $template_id, 'value' => $member['value'],
                                    'bgtype' => $bgtype, 'classtext' => $classtext, 'classcolor' => $classcolor, 'foottext' => $foottext,
                                    'footcolor' => $footcolor, 'footicon' => $footicon, 'giftbgc' => $giftbgc, 'giftcolor' => $giftcolor,
                                    'classbgc' => $classbgc, 'amountcolor' => $amountcolor
                                ]
                            );
                        }
                        foreach ($promotions as $promotion) {
                            if (empty($promotion['amount']) && $promotion['status'] == 1) {
                                throw new \Exception($promotion['name'] . ':充值价格不能为空');
                            }
                            $amount = bcmul($promotion['amount'], 100);
                            $bgtype = $promotion['bgtype'] ?? '';
                            $classtext = $promotion['classtext'] ?? '';
                            $classcolor = $promotion['classcolor'] ?? '';
                            $foottext = $promotion['foottext'] ?? '';
                            $footcolor = $promotion['footcolor'] ?? '';
                            $footicon = $promotion['footicon'] ?? '';
                            $giftbgc = $promotion['giftbgc'] ?? '';
                            $giftcolor = $promotion['giftcolor'] ?? '';
                            $classbgc = $promotion['classbgc'] ?? '';
                            $amountcolor = $promotion['amountcolor'] ?? '';
                            RechargeGoods::create(
                                [
                                    'name' => $promotion['name'], 'amount' => $amount, 'type' => 2, 'status' => $templateStatus[$promotion['status']],
                                    'day' => $promotion['day'], 'recharge_template_id' => $template_id, 'value' => $promotion['value'],
                                    'bgtype' => $bgtype, 'classtext' => $classtext, 'classcolor' => $classcolor, 'foottext' => $foottext,
                                    'footcolor' => $footcolor, 'footicon' => $footicon, 'giftbgc' => $giftbgc, 'giftcolor' => $giftcolor,
                                    'classbgc' => $classbgc, 'amountcolor' => $amountcolor
                                ]
                            );
                        }
                    }

                    //删除对应表的数据
                    RechargeTemplateGoodsRel::query()->where(['recharge_template_id' => $template_id])->delete();
                    //查找对应的唯一值的数据
                    $rechargeGoodsData = RechargeGoods::query()->whereIn('value', $sortInfo)->where(['recharge_template_id' => $template_id, 'status' => RechargeGoods::STATUS_SHOW])->select(['id', 'value'])->get()->toArray();
                    if (empty($rechargeGoodsData)) {
                        throw new \Exception('没有对应的模板数据');
                    } else {

                        $goodsKeyVal = array();

                        foreach ($rechargeGoodsData as $rechargeGoodsValue) {
                            $goodsKeyVal[$rechargeGoodsValue['value']] = $rechargeGoodsValue['id'];
                        }
                        $sortNum = count($sortInfo);
                        foreach ($sortInfo as $sortValue) {
                            if (!empty($goodsKeyVal[$sortValue])) {
                                RechargeTemplateGoodsRel::create(
                                    [
                                        'recharge_goods_id' => $goodsKeyVal[$sortValue],
                                        'recharge_template_id' => $template_id,
                                        'sort' => $sortNum,
                                    ]
                                );
                                $sortNum = $sortNum - 1;
                            }
                        }
                    }
                }else if($temp_type == 2){
                    $retention = $postData['retention'];
                    $retention = $retention[0];
                    if(empty($retention['amount'])) {
                        throw new \Exception($retention['name'].':充值价格不能为空');
                    }
                    $amount = bcmul($retention['amount'],100);
                    $name = $retention['name'] ?? '';
                    $give_coin = $retention['give_coin'] ?? '';
                    $status = $templateStatus[$retention['status']] ?? 0;
                    $day = $retention['day'] ?? 0;
                    $value = $retention['value'] ?? '';
                    $bgtype  = $retention['bgtype'] ?? '';
                    $footicon  = $retention['footicon'] ?? '';
                    $type = 1;
                    if($is_update){
                        $updateData = [
                            'amount' => $amount,
                            'ml_coin' => $amount,
                            'give_coin' => $give_coin,
                            'status' => $status,
                        ];
                        RechargeGoods::query()->where(['id' => $retention['id']])->update($updateData);
                    }else{
                        $rechargeGoodsData = RechargeGoods::create(
                            [
                                'name' => $name, 'amount' => $amount, 'ml_coin' => $amount,'template_type' => 2,
                                'give_coin' => $give_coin, 'type' => $type, 'status' => $status,'footicon'=>$footicon,
                                'day' => $day, 'recharge_template_id' => $template_id,'value' => $value, 'bgtype'=>$bgtype,
                            ]
                        );
                        $goods_id = $rechargeGoodsData->id;

                        RechargeTemplateGoodsRel::create(
                            [
                                'recharge_goods_id' =>$goods_id,
                                'recharge_template_id' => $template_id,
                                'sort' => 1,
                            ]
                        );
                    }
                }
                Db::commit();
                return $this->response(SystemCode::SYSTEM_SUCCESS);
            }else{

                Db::rollBack();
                return $this->response(SystemCode::SYSTEM_ERROR,'保存错误');
            }

        }catch (\Exception $e){
            Db::rollBack();
            return $this->response(SystemCode::SYSTEM_ERROR,$e->getMessage());
        }
    }

    public function recombinationData(Array $selectData){
        $result = array();
        $result['name'] = $selectData['name'];
        $result['status'] = $selectData['status'];
        $result['company_id'] = $selectData['company_id'];
        $result['applet_id'] = $selectData['applet_id'];
        $result['type'] = $selectData['type'];
        $rechargeGoods = RechargeGoods::query()->where(['recharge_template_id' => $selectData['id']])->get()->toArray();

        $member = array();
        $convention = array();
        $promotion = array();
        $retention = array();
        $sortInfo = array();
        $typeName = [1 => 'convention' ,3 => 'member',2 => 'promotion'];
        if(!empty($rechargeGoods)){
            foreach ($rechargeGoods as $goods){

                //2024-03-14 挽留模板
                if($goods['template_type'] == 2){
                    $type_name = 'retention';
                }else{
                    $type_name = $typeName[$goods['type']];
                }
                $$type_name[] = [
                    'id' => $goods['id'],
                    'name' => $goods['name'],
                    'amount' =>  sprintf("%.2f",$goods['amount']/100),
                    'give_coin' => $goods['give_coin'],
                    'status' => ($goods['status'] == 1) ? true : false,
                    'type' => $goods['type'],
                    'day' => $goods['day'],
                    'value' => $goods['value'],
                    'bgtype' => $goods['bgtype'],
                    'classtext' => $goods['classtext'],
                    'classcolor' => $goods['classcolor'],
                    'foottext' => $goods['foottext'],
                    'footcolor' => $goods['footcolor'],
                    'footicon' => $goods['footicon'],
                    'giftbgc' => $goods['giftbgc'],
                    'giftcolor' => $goods['giftcolor'],
                    'classbgc' => $goods['classbgc'],
                    'amountcolor' => $goods['amountcolor']
                ];
            }
        }

        $rechargeGoodsIds = RechargeTemplateGoodsRel::query()->where(['recharge_template_id' =>$selectData['id']])->orderBy('sort','desc')->pluck('recharge_goods_id');


        $rechargeGoodsSortData = RechargeGoods::query()->whereIn('id',$rechargeGoodsIds)->select(['id','value'])->get()->toArray();
        $goodsKeyVal = array();
        foreach ($rechargeGoodsSortData as $goodsVal){
            $goodsKeyVal[$goodsVal['id']] = $goodsVal['value'];
        }
        foreach ($rechargeGoodsIds as $good_id){
            if(!empty($goodsKeyVal[$good_id])){
                $sortInfo[] = $goodsKeyVal[$good_id];
            }
        }
        $result['retention'] = $retention;
        $result['convention'] = $convention;
        $result['member'] = $member;
        $result['promotion'] = $promotion;
        $result['sortInfo'] = $sortInfo;
        return $result;
    }



    public function copy(RechargeTemplate $rechargeInfo = null){
        Db::beginTransaction();
        try {

            //
            $name = $rechargeInfo->name;
            //存在
            if(strpos($name,'_')){

                $arr = explode('_',$name);
                $num = count($arr);
                $name = '';
                foreach ($arr as $key => $val){
                    if($key+1 == $num){
                        $name.='_'.(int)$val+1;
                    }else{
                        $name .= $val;
                    }
                }
            }else{
                $name = $name.'_1';
            }
            $old_id = $rechargeInfo->id;
            $model = new RechargeTemplate();
            $model->name = $name;
            $model->status = $rechargeInfo->status;
            $model->sort = $rechargeInfo->sort;
            $model->company_id = $rechargeInfo->company_id;
            $model->applet_id = $rechargeInfo->applet_id;
            $model->is_default = $rechargeInfo->is_default;
            $model->user_id = $rechargeInfo->user_id;
            $model->type = $rechargeInfo->type;
            if($model->save()){
                $recharge_template_id = $model->id;
                $goodsData = RechargeGoods::query()->where(['recharge_template_id' => $old_id])->get();
                if(!empty($goodsData)){
                    $relData = RechargeTemplateGoodsRel::query()->where(['recharge_template_id' => $old_id])->get();

                    $relGoodsIds = [];
                    $relCopyData = [];
                    foreach ($relData as $relDatum){
                        $relGoodsIds[] = $relDatum->recharge_goods_id;
                        $relCopyData[$relDatum->recharge_goods_id] = $relDatum;
                    }

                    foreach ($goodsData as $goodsDatum){

                        $is_have = false;
                        if(!empty($relGoodsIds)){
                            if(in_array($goodsDatum->id,$relGoodsIds)){
                                $is_have = true;
                            }
                        }
                        $goodModel = new RechargeGoods();
                        $goodModel->name = $goodsDatum->name;
                        $goodModel->amount = $goodsDatum->amount;
                        $goodModel->ml_coin = $goodsDatum->ml_coin;
                        $goodModel->give_coin = $goodsDatum->give_coin;
                        $goodModel->prompt1 = $goodsDatum->prompt1;
                        $goodModel->prompt2 = $goodsDatum->prompt2;
                        $goodModel->day = $goodsDatum->day;
                        $goodModel->type = $goodsDatum->type;
                        $goodModel->status = $goodsDatum->status;
                        $goodModel->deleted = $goodsDatum->deleted;
                        $goodModel->sort = $goodsDatum->sort;
                        $goodModel->base_angle = $goodsDatum->base_angle;
                        $goodModel->recharge_template_id = $recharge_template_id;
                        $goodModel->value = $this->generateRandomString(58);
                        $goodModel->bgtype = $goodsDatum->bgtype;
                        $goodModel->classtext = $goodsDatum->classtext;
                        $goodModel->classcolor = $goodsDatum->nameclasscolor;
                        $goodModel->foottext = $goodsDatum->foottext;
                        $goodModel->footcolor = $goodsDatum->footcolor;
                        $goodModel->footicon = $goodsDatum->footicon;
                        $goodModel->giftbgc = $goodsDatum->giftbgc;
                        $goodModel->giftcolor = $goodsDatum->giftcolor;
                        $goodModel->classbgc = $goodsDatum->classbgc;
                        $goodModel->amountcolor = $goodsDatum->amountcolor;
                        $goodModel->template_type = $goodsDatum->template_type;
                        $goodModel->save();
                        if($is_have){

                           $relCopyInfo =  $relCopyData[$goodsDatum->id];

                            $relModel = new RechargeTemplateGoodsRel();
                            $relModel->is_highlight = $relCopyInfo->is_highlight;
                            $relModel->is_default = $relCopyInfo->is_default;
                            $relModel->recharge_goods_id = $goodModel->id;
                            $relModel->sort = $relCopyInfo->sort;
                            $relModel->recharge_template_id = $recharge_template_id;
                            $relModel->is_pay = $relCopyInfo->is_pay;
                            $relModel->save();
                        }
                    }
                }

                Db::commit();
                return $this->response(SystemCode::SYSTEM_SUCCESS);
            }else{
                Db::rollBack();
                return $this->response(SystemCode::SYSTEM_ERROR,'复制失败');
            }
        }catch (\Exception $e){
            Db::rollBack();
            return $this->response(SystemCode::SYSTEM_ERROR,$e->getMessage());
        }
    }
}